package com.wwz.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.feign.FileClients;
import com.wwz.mapper.PersonnelMapper;
import com.wwz.pojo.FileInfo;
import com.wwz.pojo.Personnel;
import com.wwz.pojo.model.LoginModel;
import com.wwz.pojo.query.PersonnelQuery;
import com.wwz.util.JobUtils;
import com.wwz.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 人事管理接口实现
 * @date 2022/03/24 31:02:10
 */
@Service
public class PersonnelServiceImpl extends ServiceImpl<PersonnelMapper, Personnel> implements IPersonnelService {

    @Resource
    private FileClients fileClients;

    @Override
    public IPage<Personnel> toPage(PersonnelQuery query) {

        LambdaQueryWrapper<Personnel> wrapper = new LambdaQueryWrapper<>();
        if (StrUtil.isNotEmpty(query.getName())){
            wrapper.like(Personnel::getName,query.getName());

        }
        if (StrUtil.isNotEmpty(query.getJobNumber())){
            wrapper.like(Personnel::getJobNumber,query.getJobNumber());
        }
        wrapper.orderByDesc(Personnel::getCreateTime);

        IPage<Personnel> page = this.page(query.toPage(), wrapper);

        // 文件info填充
        List<FileInfo> result = fileClients.fileList().getResult();
        Map<String, List<FileInfo>> fileMap = result.stream().collect(Collectors.groupingBy(FileInfo::getDataId));
        for (Personnel personnel : page.getRecords()) {
            List<FileInfo> fileInfos = fileMap.get(personnel.getPhone());
            if (CollUtil.isNotEmpty(fileInfos)){
                personnel.setFileInfo(fileInfos.get(0));
            }
        }

        return page;
    }

    @Override
    public void addOrUpdate(Personnel personnel) {

        Personnel one = this.getById(personnel);
        if (one == null){
            personnel.setId(StrUtil.uuid());
            personnel.setCreateTime(new Date());
            save(personnel);
            return;
        }
        updateById(personnel);
    }

    @Override
    public LoginModel login(LoginModel loginModel) {
        String userName = loginModel.getUserName();
        String password = loginModel.getPassWord();

        Boolean rememberMe = loginModel.getRememberMe();
        if (rememberMe == null){
            rememberMe = false;
        }

        Personnel personnel = getOne(new LambdaQueryWrapper<Personnel>()
                        .eq(Personnel::getName, userName)
                        .eq(Personnel::getPassWord, password)
                , false);

        if (personnel == null){
            return null;
        }

        StpUtil.login(personnel.getId(),rememberMe);

        loginModel.setPersonnel(personnel);
        loginModel.setSatoken(StpUtil.getTokenValue());

        return loginModel;
    }


}
